package cn.fu.robot;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONObject;
import cn.fu.util.Const;


/**
 * 
* @ClassName: TulingApiProcess 
* @Description: 机器人自动回复 
* @author zhangxiaofu
* @date 2016年5月15日 下午1:48:48 
*
 */
public class TulingApiProcess {
	/**
	* @Title: getTulingResult 
	* @Description: 调用图灵机器人api接口，获取智能回复内容，解析获取自己所需结果
	* @param @param content
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	public static String getTulingResult(String content){
		//图灵机器人数据库接口
		StringBuffer sb = new StringBuffer();
		String result = "";
		try {
			String INFO = URLEncoder.encode(content, "utf-8"); 
			String getURL = Const.API_ADDRESS+"?key="+ Const.API_KEY + "&info=" + INFO;
			URL getUrl = new URL(getURL);
			HttpURLConnection connection = (HttpURLConnection) getUrl
					.openConnection();
			connection.connect();

			// 取得输入流，并使用Reader读取
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "utf-8"));

			String line = "";
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			reader.close();
			// 断开连接
			connection.disconnect();
			JSONObject json = new JSONObject(sb.toString());
			result = json.getString("text");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}